<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.2">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"wrr123.github.io","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.json"};
  </script>

  <meta name="description" content="什么是 FreeMarkerFreeMarker 是一种模板引擎：一种基于模板的、用来生成输出文件(任何来自于 HTML 格式的文本用来自动生成源代码)的通用工具。 它是为 Java 程序员提供的一个开发包或者说是类库。 它不是面向最终用户，而是为程序员提供的可以嵌入他们的开发产品的一款应用程序。">
<meta property="og:type" content="article">
<meta property="og:title" content="freemarker快速入门">
<meta property="og:url" content="https://wrr123.github.io/2020/08/12/freemarker%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8/index.html">
<meta property="og:site_name" content="一缕烟气">
<meta property="og:description" content="什么是 FreeMarkerFreeMarker 是一种模板引擎：一种基于模板的、用来生成输出文件(任何来自于 HTML 格式的文本用来自动生成源代码)的通用工具。 它是为 Java 程序员提供的一个开发包或者说是类库。 它不是面向最终用户，而是为程序员提供的可以嵌入他们的开发产品的一款应用程序。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-08-12T10:20:22.000Z">
<meta property="article:modified_time" content="2022-02-18T02:52:04.415Z">
<meta property="article:author" content="田园隐士">
<meta property="article:tag" content="java">
<meta property="article:tag" content="freemarker">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://wrr123.github.io/2020/08/12/freemarker%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>freemarker快速入门 | 一缕烟气</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="一缕烟气" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">一缕烟气</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">沧海月明珠有泪，蓝田日暖玉生烟</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://wrr123.github.io/2020/08/12/freemarker%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="田园隐士">
      <meta itemprop="description" content="talk is cheap, show me the code">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一缕烟气">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          freemarker快速入门
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-08-12 18:20:22" itemprop="dateCreated datePublished" datetime="2020-08-12T18:20:22+08:00">2020-08-12</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2022-02-18 10:52:04" itemprop="dateModified" datetime="2022-02-18T10:52:04+08:00">2022-02-18</time>
              </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>6.1k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>6 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h4 id="什么是-FreeMarker"><a href="#什么是-FreeMarker" class="headerlink" title="什么是 FreeMarker"></a>什么是 FreeMarker</h4><p>FreeMarker 是一种模板引擎：一种基于模板的、用来生成输出文件(任何来自于 HTML 格式的文本用来自动生成源代码)的通用工具。</p>
<p>它是为 Java 程序员提供的一个开发包或者说是类库。</p>
<p>它不是面向最终用户，而是为程序员提供的可以嵌入他们的开发产品的一款应用程序。</p>
<span id="more"></span>
<h4 id="搭建-SpringMVC-FreeMarker-环境"><a href="#搭建-SpringMVC-FreeMarker-环境" class="headerlink" title="搭建 SpringMVC + FreeMarker 环境"></a>搭建 SpringMVC + FreeMarker 环境</h4><h5 id="一个-springmvc-xml-的配置栗子："><a href="#一个-springmvc-xml-的配置栗子：" class="headerlink" title="一个 springmvc.xml 的配置栗子："></a>一个 springmvc.xml 的配置栗子：</h5><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--对模型视图名称的解析，即在模型视图名称添加前后缀 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;viewResolver&quot;</span></span></span><br><span class="line"><span class="tag">	<span class="attr">class</span>=<span class="string">&quot;org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;viewClass&quot;</span></span></span><br><span class="line"><span class="tag">			<span class="attr">value</span>=<span class="string">&quot;org.springframework.web.servlet.view.freemarker.FreeMarkerView&quot;</span> /&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;prefix&quot;</span> <span class="attr">value</span>=<span class="string">&quot;/&quot;</span> /&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;suffix&quot;</span> <span class="attr">value</span>=<span class="string">&quot;.html&quot;</span> /&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;contentType&quot;</span> <span class="attr">value</span>=<span class="string">&quot;text/html;charset=utf-8&quot;</span> /&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;exposeRequestAttributes&quot;</span> <span class="attr">value</span>=<span class="string">&quot;true&quot;</span> /&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;exposeSessionAttributes&quot;</span> <span class="attr">value</span>=<span class="string">&quot;true&quot;</span> /&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;exposeSpringMacroHelpers&quot;</span> <span class="attr">value</span>=<span class="string">&quot;true&quot;</span> /&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="comment">&lt;!-- 定义freemarker参数文件并载入 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;freemarkerConfiguration&quot;</span></span></span><br><span class="line"><span class="tag">		<span class="attr">class</span>=<span class="string">&quot;org.springframework.beans.factory.config.PropertiesFactoryBean&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;location&quot;</span> <span class="attr">value</span>=<span class="string">&quot;classpath:freemarker.properties&quot;</span> /&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- 定义freemarker配置，包括模板根路径、参数 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;freemarkerConfig&quot;</span></span></span><br><span class="line"><span class="tag">		<span class="attr">class</span>=<span class="string">&quot;org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer&quot;</span>&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- 注意： templateLoaderPath必须设置，否则freemarker找不到模板位置 --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;templateLoaderPath&quot;</span> <span class="attr">value</span>=<span class="string">&quot;/WEB-INF&quot;</span> /&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;freemarkerSettings&quot;</span> <span class="attr">ref</span>=<span class="string">&quot;freemarkerConfiguration&quot;</span> /&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h5 id="一个-freemarker-properties-的栗子："><a href="#一个-freemarker-properties-的栗子：" class="headerlink" title="一个 freemarker.properties 的栗子："></a>一个 freemarker.properties 的栗子：</h5><figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">template_update_delay</span>=<span class="string">0</span></span><br><span class="line"><span class="attr">defaultEncoding</span>=<span class="string">UTF-8</span></span><br><span class="line"><span class="attr">url_escaping_charset</span>=<span class="string">UTF-8</span></span><br><span class="line"><span class="attr">locale</span>=<span class="string">zh_CN</span></span><br><span class="line"><span class="attr">boolean_format</span>=<span class="string">true,false</span></span><br><span class="line"><span class="attr">datetime_format</span>=<span class="string">yyyy-MM-dd HH:mm:ss</span></span><br><span class="line"><span class="attr">date_format</span>=<span class="string">yyyy-MM-dd</span></span><br><span class="line"><span class="attr">time_format</span>=<span class="string">HH:mm:ss</span></span><br><span class="line"><span class="attr">number_format</span>=<span class="string">0.######</span></span><br><span class="line"><span class="attr">whitespace_stripping</span>=<span class="string">true</span></span><br></pre></td></tr></table></figure>
<h4 id="打印值"><a href="#打印值" class="headerlink" title="打印值"></a>打印值</h4><h5 id="字符串"><a href="#字符串" class="headerlink" title="字符串"></a>字符串</h5><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&lt;#assign name=&quot;hello&quot;&gt;</span><br><span class="line">$&#123;name&#125; <span class="comment">&lt;!-- 输出 hello --&gt;</span></span><br></pre></td></tr></table></figure>
<h5 id="数值"><a href="#数值" class="headerlink" title="数值"></a>数值</h5><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;#assign number=10&gt;</span><br><span class="line">	$&#123;number&#125;</span><br><span class="line">    <span class="comment">&lt;!--输出10 --&gt;</span></span><br><span class="line">&lt;#assign number2=2.5&gt;</span><br><span class="line">   		$&#123;number2&#125;</span><br><span class="line">    <span class="comment">&lt;!--输出2.5 --&gt;</span></span><br><span class="line">&lt;#assign avg = number/number2&gt;</span><br><span class="line">    	$&#123;avg&#125;</span><br><span class="line"><span class="comment">&lt;!--输出4 --&gt;</span></span><br></pre></td></tr></table></figure>
<h5 id="boolean"><a href="#boolean" class="headerlink" title="boolean"></a>boolean</h5><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;#assign flag = true&gt;</span><br><span class="line">	$&#123;flag&#125;</span><br><span class="line"><span class="comment">&lt;!-- 	输出true --&gt;</span></span><br><span class="line">	&lt;#assign flag2 = false&gt;</span><br><span class="line">	$&#123;flag2&#125;</span><br><span class="line"><span class="comment">&lt;!-- 	什么也没有输出 为空字符串--&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;#assign flag3 = &quot;true&quot;&gt;</span><br><span class="line">	$&#123;flag3&#125;</span><br><span class="line"><span class="comment">&lt;!-- 	输出为true --&gt;</span></span><br><span class="line">	&lt;#assign flag4 = &quot;false&quot;&gt;</span><br><span class="line">	$&#123;flag4&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;#if flag3&gt; 为真 &lt;/#if&gt; <span class="comment">&lt;!-- 打印为真，freemarker将字符串&quot;true&quot;转换为boolean --&gt;</span></span><br></pre></td></tr></table></figure>
<h5 id="运算符"><a href="#运算符" class="headerlink" title="运算符"></a>运算符</h5><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;#assign x=2&gt;</span><br><span class="line">    $&#123;100-2&#125;</span><br><span class="line">   <span class="comment">&lt;!--  减   打印98 --&gt;</span></span><br><span class="line">    $&#123;10/2&#125;</span><br><span class="line">   <span class="comment">&lt;!--  除  打印5 --&gt;</span></span><br><span class="line">    $&#123;10*2&#125;</span><br><span class="line">   <span class="comment">&lt;!--   乘  打印20 --&gt;</span></span><br><span class="line">    $&#123;9%2&#125;</span><br><span class="line">   <span class="comment">&lt;!--   取余  打印1 --&gt;</span></span><br></pre></td></tr></table></figure>
<h5 id="流程控制"><a href="#流程控制" class="headerlink" title="流程控制"></a>流程控制</h5><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">&lt;#assign x2 = 1&gt;</span><br><span class="line">	&lt;#assign x3 = 5&gt;</span><br><span class="line">	&lt;#assign x4 =6&gt;</span><br><span class="line">	&lt;#if (x2&lt;x3) &gt;</span><br><span class="line">		A</span><br><span class="line">	&lt;/#if&gt;</span><br><span class="line"><span class="comment">&lt;!-- 	打印A，注意此处一定要加括号 --&gt;</span></span><br><span class="line">	&lt;#if (x2&gt;x3)&gt;</span><br><span class="line">		B</span><br><span class="line">	&lt;#else&gt;</span><br><span class="line">	    C</span><br><span class="line">	&lt;/#if&gt;</span><br><span class="line"><span class="comment">&lt;!-- 	打印C --&gt;</span></span><br><span class="line">	&lt;#if (x2&gt;x3)&gt;</span><br><span class="line">		A</span><br><span class="line">	&lt;#elseif (x2&gt;x4)&gt;</span><br><span class="line">		B</span><br><span class="line">	&lt;#elseif (x2&lt;x4)&gt;</span><br><span class="line">		C</span><br><span class="line">	&lt;/#if&gt;</span><br><span class="line"><span class="comment">&lt;!-- 	打印C --&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;#assign x5 =5&gt;</span><br><span class="line">	&lt;#switch x5&gt;</span><br><span class="line">		&lt;#case 10&gt;</span><br><span class="line">		 A</span><br><span class="line">		&lt;#break&gt;</span><br><span class="line">		</span><br><span class="line">		&lt;#case 5&gt;</span><br><span class="line">		 B</span><br><span class="line">		&lt;#break&gt;</span><br><span class="line">		&lt;#default&gt;</span><br><span class="line">		 C</span><br><span class="line">	&lt;/#switch&gt;</span><br><span class="line">  <span class="comment">&lt;!-- 	打印5 --&gt;</span></span><br></pre></td></tr></table></figure>
<h5 id="遍历"><a href="#遍历" class="headerlink" title="遍历"></a>遍历</h5><h6 id="List，-Set"><a href="#List，-Set" class="headerlink" title="List， Set"></a>List， Set</h6><p>后台传入一个 userList 的 list 或者 set</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;#list userList as user&gt;</span><br><span class="line">  $&#123;user.name&#125;</span><br><span class="line">&lt;/#list&gt;</span><br><span class="line"><span class="comment">&lt;!--打印出用户名列表--&gt;</span></span><br></pre></td></tr></table></figure>
<h6 id="Map"><a href="#Map" class="headerlink" title="Map"></a>Map</h6><p>map 为后台传入的 Map 集合</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;#list map?keys as k&gt;</span><br><span class="line">		$&#123;k&#125;</span><br><span class="line"><span class="comment">&lt;!-- 		键 --&gt;</span></span><br><span class="line">		$&#123;map[k]&#125;</span><br><span class="line"><span class="comment">&lt;!-- 		值 --&gt;</span></span><br><span class="line">&lt;/#list&gt;</span><br></pre></td></tr></table></figure>
<h5 id="处理空值"><a href="#处理空值" class="headerlink" title="处理空值"></a>处理空值</h5><blockquote>
<p><strong>可在freemarker.properties中加一句</strong>classic_compatible=true如果对象为空将不会报错</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;#if user??&gt;</span><br><span class="line">&lt;/#if&gt;</span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- 判断user对象是否为空,如果不为空执行if语句中的代码--&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 在变量后面加！处理变量值为空的情况--&gt;</span></span><br><span class="line">$&#123;user.name!&#125;</span><br><span class="line"><span class="comment">&lt;!-- 判断name是否为空 如果为空将什么也不显示--&gt;</span></span><br><span class="line">$&#123;(user.name)!&#125;</span><br><span class="line"><span class="comment">&lt;!-- 判断user和name是否都为空，如果有任意一个为空什么都不显示   如果你有多个对象连环调用属性请一定要加()不然freemarker只会判断最后一个变量，否则报错--&gt;</span></span><br><span class="line">$&#123;(user.name)!&quot;您还没有登录&quot;&#125;</span><br><span class="line"><span class="comment">&lt;!-- 如果变量为空的情况下，将显示默认值--&gt;</span></span><br></pre></td></tr></table></figure>
<h5 id="内置函数"><a href="#内置函数" class="headerlink" title="内置函数"></a>内置函数</h5><h6 id="集合"><a href="#集合" class="headerlink" title="集合"></a>集合</h6><p>userList 为后台传入的对象集合</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">&lt;#list userList?reverse  as li&gt;</span><br><span class="line">	$&#123;li.name&#125;</span><br><span class="line">	&lt;/#list&gt;</span><br><span class="line">   <span class="comment">&lt;!-- 	 ?reverse      将对象集合顺序反转 --&gt;</span></span><br><span class="line">	</span><br><span class="line">	$&#123;(userList?first).name&#125;</span><br><span class="line">   <span class="comment">&lt;!-- 	 ?first  打印集合中第一个user对象的name --&gt;</span></span><br><span class="line">	 </span><br><span class="line">	$&#123;(userList?last).name&#125;</span><br><span class="line">   <span class="comment">&lt;!-- 	?last      打印集合中最后一个user对象的name --&gt;</span></span><br><span class="line">	</span><br><span class="line">	$&#123;userList?size&#125;</span><br><span class="line">   <span class="comment">&lt;!-- 	?size    打印集合的长度 --&gt;</span></span><br><span class="line">	</span><br><span class="line">	&lt;#list userList?sort  as li&gt;</span><br><span class="line">	$&#123;li.name&#125;</span><br><span class="line">	&lt;/#list&gt;<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 	将对象转换为字符串  顺序排序 --&gt;</span></span><br><span class="line">	&lt;#list userList?sort_by(&quot;id&quot;)  as li&gt;</span><br><span class="line">	$&#123;li.name&#125;</span><br><span class="line">	&lt;/#list&gt;<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 	将对象中的属性值进行排序, 当前为user中的id排序 --&gt;</span></span><br></pre></td></tr></table></figure>
<h6 id="字符串-1"><a href="#字符串-1" class="headerlink" title="字符串"></a>字符串</h6><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line">$&#123;&quot;string&quot;?substring(0)&#125;</span><br><span class="line"><span class="comment">&lt;!-- 从索引0开始截取字符串  为string --&gt;</span></span><br><span class="line">$&#123;&quot;string&quot;?substring(1)&#125;</span><br><span class="line"><span class="comment">&lt;!-- 从索引1开始截取字符串 为tring --&gt;</span></span><br><span class="line"></span><br><span class="line">$&#123;&quot;string&quot;?substring(0,1)&#125;</span><br><span class="line"><span class="comment">&lt;!-- 从索引0到1截取字符串  为 s --&gt;</span></span><br><span class="line">$&#123;&quot;string&quot;?substring(1,2)&#125;</span><br><span class="line"><span class="comment">&lt;!-- 从索引1到2截取字符串为 t --&gt;</span></span><br><span class="line"></span><br><span class="line">$&#123;&quot;string&quot;?cap_first&#125;</span><br><span class="line"><span class="comment">&lt;!-- 将字符串首字母变为大写  String--&gt;</span></span><br><span class="line">$&#123;&quot;String&quot;?uncap_first&#125;</span><br><span class="line"><span class="comment">&lt;!-- 将字符串首字母变为小写 string--&gt;</span></span><br><span class="line">$&#123;&quot;string&quot;?upper_case&#125;</span><br><span class="line"><span class="comment">&lt;!-- 将字符串全部转换为大写 --&gt;</span></span><br><span class="line">$&#123;&quot;STRINg&quot;?lower_case&#125;</span><br><span class="line"><span class="comment">&lt;!-- 将字符串全部转换为小写 --&gt;</span></span><br><span class="line">$&#123;&quot;2014-11-11 9:0:0&quot;?date(&quot;yyyy-MM-dd&quot;)&#125;</span><br><span class="line"><span class="comment">&lt;!-- 输出为2014-11-11 --&gt;</span></span><br><span class="line">$&#123;&quot;string&quot;?ends_with(&quot;ing&quot;)&#125;</span><br><span class="line"><span class="comment">&lt;!-- 判断字符串 是否已某段字符串结尾               打印true --&gt;</span></span><br><span class="line"></span><br><span class="line">$&#123;&quot;string&quot;?index_of(&quot;tr&quot;)&#125;</span><br><span class="line"><span class="comment">&lt;!-- 	判断某段字符串在一段字符串中出现的索引位置，如果没有返回-1           打印1 --&gt;</span></span><br><span class="line">$&#123;&quot;string&quot;?contains(&#x27;ing&#x27;)&#125;</span><br><span class="line"><span class="comment">&lt;!-- 判断一个字符串中是否包含某段字符串  返回true或false --&gt;</span></span><br><span class="line"></span><br><span class="line">$&#123;&quot;strabgab&quot;?replace(&quot;ab&quot;,&quot;in&quot;)&#125;</span><br><span class="line"><span class="comment">&lt;!-- 将字符串总包含的某段字符串全部替换掉  结果为stringin--&gt;</span></span><br><span class="line"></span><br><span class="line">&lt;#list &quot;this,is,freemaker&quot;?split(&quot;,&quot;) as s&gt;</span><br><span class="line">    $&#123;s&#125;</span><br><span class="line">&lt;/#list&gt;</span><br><span class="line"><span class="comment">&lt;!-- 使用指定的符号将字符串分割为数组 --&gt;</span></span><br><span class="line">$&#123;&quot;  String       &quot;?trim&#125;</span><br><span class="line"><span class="comment">&lt;!-- 去掉首位空格 --&gt;</span></span><br><span class="line"></span><br><span class="line">&lt;#assign num = 1&gt;</span><br><span class="line">$&#123;num?string.number&#125;</span><br><span class="line"><span class="comment">&lt;!-- 转换为数字格式  1 --&gt;</span></span><br><span class="line">$&#123;num?string.currency&#125;</span><br><span class="line"><span class="comment">&lt;!-- 转换为货币格式  ￥1.00 --&gt;</span></span><br><span class="line">$&#123;num?string. percent&#125;</span><br><span class="line"><span class="comment">&lt;!-- 转换为百分比格式 100% --&gt;</span></span><br><span class="line"></span><br><span class="line">&lt;#assign flag = true&gt;</span><br><span class="line">$&#123;flag?string(&quot;yes&quot;,&quot;no&quot;)&#125;</span><br><span class="line"><span class="comment">&lt;!-- 根据boolean类型的真假值来输出相应的字符串 --&gt;</span></span><br></pre></td></tr></table></figure>

    </div>

    
    
    
        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/java/" rel="tag"># java</a>
              <a href="/tags/freemarker/" rel="tag"># freemarker</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/08/12/mybatis-plus%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/" rel="prev" title="mybatis-plus基本使用">
      <i class="fa fa-chevron-left"></i> mybatis-plus基本使用
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/08/13/StringBuffer-StringBuilder%E5%8C%BA%E5%88%AB/" rel="next" title="StringBuffer-StringBuilder区别">
      StringBuffer-StringBuilder区别 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF-FreeMarker"><span class="nav-number">1.</span> <span class="nav-text">什么是 FreeMarker</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%90%AD%E5%BB%BA-SpringMVC-FreeMarker-%E7%8E%AF%E5%A2%83"><span class="nav-number">2.</span> <span class="nav-text">搭建 SpringMVC + FreeMarker 环境</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E4%B8%80%E4%B8%AA-springmvc-xml-%E7%9A%84%E9%85%8D%E7%BD%AE%E6%A0%97%E5%AD%90%EF%BC%9A"><span class="nav-number">2.1.</span> <span class="nav-text">一个 springmvc.xml 的配置栗子：</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E4%B8%80%E4%B8%AA-freemarker-properties-%E7%9A%84%E6%A0%97%E5%AD%90%EF%BC%9A"><span class="nav-number">2.2.</span> <span class="nav-text">一个 freemarker.properties 的栗子：</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%89%93%E5%8D%B0%E5%80%BC"><span class="nav-number">3.</span> <span class="nav-text">打印值</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="nav-number">3.1.</span> <span class="nav-text">字符串</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%95%B0%E5%80%BC"><span class="nav-number">3.2.</span> <span class="nav-text">数值</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#boolean"><span class="nav-number">3.3.</span> <span class="nav-text">boolean</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-number">3.4.</span> <span class="nav-text">运算符</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6"><span class="nav-number">3.5.</span> <span class="nav-text">流程控制</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E9%81%8D%E5%8E%86"><span class="nav-number">3.6.</span> <span class="nav-text">遍历</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#List%EF%BC%8C-Set"><span class="nav-number">3.6.1.</span> <span class="nav-text">List， Set</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#Map"><span class="nav-number">3.6.2.</span> <span class="nav-text">Map</span></a></li></ol></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E5%A4%84%E7%90%86%E7%A9%BA%E5%80%BC"><span class="nav-number">3.7.</span> <span class="nav-text">处理空值</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0"><span class="nav-number">3.8.</span> <span class="nav-text">内置函数</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#%E9%9B%86%E5%90%88"><span class="nav-number">3.8.1.</span> <span class="nav-text">集合</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2-1"><span class="nav-number">3.8.2.</span> <span class="nav-text">字符串</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">田园隐士</p>
  <div class="site-description" itemprop="description">talk is cheap, show me the code</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">347</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
        <span class="site-state-item-count">53</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
        <span class="site-state-item-count">115</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">田园隐士</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">587k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">8:53</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  

  

</body>
</html>
